<%@page import="javax.sql.DataSource"%>
<%@page import="javax.naming.InitialContext"%>
<%@page import="javax.naming.Context"%>
<%@page import="java.sql.SQLException"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.Statement"%>
<%@page import="java.sql.Connection"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
Connection conn =null;
Statement stmt=null;
ResultSet rst=null;
try {

	 Context ctx = new InitialContext();
	 DataSource ds = (DataSource) ctx.lookup("java:comp/env/jdbc/lc");
	 conn = ds.getConnection();
	 String sql ="select * from user";
	 stmt=conn.createStatement();
	 //ResultSet 来接收查询到的结果集
	  rst=stmt.executeQuery(sql);
	 //处理接收到的结果集
	 out.println("查询到的结果为：");
	 while(rst.next())
	 {
		out.print("姓名："+rst.getString("name")+" "); 
		out.print("年龄："+rst.getString("age")+" ");
		out.print("性别: "+(rst.getInt("sex")==0?"女":"男"));
		out.println("<br/>");
	 } 	
}
catch (SQLException e) {
	e.printStackTrace();
}
finally {
	//第六步  关闭连接池
	try {
		if(rst!=null&&!rst.isClosed()){
			rst.close();
		}
	} catch (SQLException e1) {
		// TODO Auto-generated catch block
		e1.printStackTrace();
	}
	try {
		if(stmt!=null&&!stmt.isClosed()){
		stmt.close();
		}
	} catch (SQLException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
    try {
    	if(conn!=null&&!conn.isClosed())
    	{
		conn.close();
    	}
	} catch (SQLException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
}

%>
</body>
</html>